$(function() {
	sendAjax();
});

var PMETHOD_HOUR = {};
var QMETHOD_HOUR = {};
var MMETHOD_HOUR = {};
var MPQ_HOUR = {};
var shooseTypeid = 1;
var listDateM = new Array();
var listDateQ = new Array();
var listDateP = new Array();
var listDateMPQ = new Array();
var listDateFiled = new Array();
var listDayFiled = new Array();
var mapsTitle = {
	1: "最近24小时数据",
	2: "最近7天数据",
	3: "最近15天数据",
	4: "最近30天数据",
	5: "自定义时间段数据"
}

function intn(size, text) {
	listDateM = [];
	listDateQ = [];
	listDateP = [];
	listDayFiled = new Array();
	listDateFiled = new Array();
	for (var i = 0; i < size; i ++) {
		PMETHOD_HOUR[(i + 1) + ""] = 0;
		QMETHOD_HOUR[(i + 1) + ""] = 0;
		MMETHOD_HOUR[(i + 1) + ""] = 0;
	}
	for (var i = 0; i < 25; i ++) {
		var d = new Date();
		listDateFiled[i] = getThisTime(d, i);
	}
}

function displayChart(ec) {
	var lineChart = ec.init(document.getElementById('showview'));
	var lineChartOtion = getLineChartOption();	
	lineChart.setOption(lineChartOtion, true); 
}

/**
 * 
 * 获得Line图的选项和数据
 * @returns {___anonymous1011_4462}
 */
function getLineChartOption() {
	var option = {
		title: {
			text: mapsTitle[shooseTypeid],
			subtext: '',
			x: 'left'
	    },
	    tooltip: {trigger: 'axis'},
		legend: {data: ['PC端', '移动端', '二维码', '所有总数']},
		toolbox: {
			show: true,
			feature: {
				mark: {show: true},
				dataView: {show: true, readOnly: false},
				magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
				restore: {show: true},
				saveAsImage: {show: true}
			}
		},
		calculable: true,
		xAxis: [{
			type: 'category',
			boundaryGap: false,
			axisLabel: {
				interval: 0,
				rotate: 40
			},
			data: listDateFiled
		}],
		yAxis: [{type : 'value'}],
		series: [
		    {
		        name: 'PC端',
		        type: 'line',
		        data: [
		            PMETHOD_HOUR['24'], PMETHOD_HOUR['23'], PMETHOD_HOUR['22'],
		            PMETHOD_HOUR['21'], PMETHOD_HOUR['20'], PMETHOD_HOUR['19'],
		            PMETHOD_HOUR['18'], PMETHOD_HOUR['17'], PMETHOD_HOUR['16'],
		            PMETHOD_HOUR['15'], PMETHOD_HOUR['14'], PMETHOD_HOUR['13'],
		            PMETHOD_HOUR['12'], PMETHOD_HOUR['11'], PMETHOD_HOUR['10'],
		            PMETHOD_HOUR['9'], PMETHOD_HOUR['8'], PMETHOD_HOUR['7'],
		            PMETHOD_HOUR['6'], PMETHOD_HOUR['5'], PMETHOD_HOUR['4'],
		            PMETHOD_HOUR['3'], PMETHOD_HOUR['2'], PMETHOD_HOUR['1'],
		            PMETHOD_HOUR['0']
				]
		    },
		    {
		        name: '移动端',
		        type: 'line',
		        data: [
		            MMETHOD_HOUR['24'], MMETHOD_HOUR['23'], MMETHOD_HOUR['22'],
		            MMETHOD_HOUR['21'], MMETHOD_HOUR['20'], MMETHOD_HOUR['19'],
		            MMETHOD_HOUR['18'], MMETHOD_HOUR['17'], MMETHOD_HOUR['16'],
		            MMETHOD_HOUR['15'], MMETHOD_HOUR['14'], MMETHOD_HOUR['13'],
		            MMETHOD_HOUR['12'], MMETHOD_HOUR['11'], MMETHOD_HOUR['10'],
		            MMETHOD_HOUR['9'], MMETHOD_HOUR['8'], MMETHOD_HOUR['7'],
		            MMETHOD_HOUR['6'], MMETHOD_HOUR['5'], MMETHOD_HOUR['4'],
		            MMETHOD_HOUR['3'], MMETHOD_HOUR['2'],
		            MMETHOD_HOUR['1'], MMETHOD_HOUR['0']
				]
		    },
		    {
		        name: '二维码',
		        type: 'line',
		        data: [
		            QMETHOD_HOUR['24'], QMETHOD_HOUR['23'], QMETHOD_HOUR['22'],
		            QMETHOD_HOUR['21'], QMETHOD_HOUR['20'], QMETHOD_HOUR['19'],
		            QMETHOD_HOUR['18'], QMETHOD_HOUR['17'], QMETHOD_HOUR['16'],
		            QMETHOD_HOUR['15'], QMETHOD_HOUR['14'], QMETHOD_HOUR['13'],
		            QMETHOD_HOUR['12'], QMETHOD_HOUR['11'], QMETHOD_HOUR['10'],
		            QMETHOD_HOUR['9'], QMETHOD_HOUR['8'], QMETHOD_HOUR['7'],
		            QMETHOD_HOUR['6'], QMETHOD_HOUR['5'], QMETHOD_HOUR['4'],
		            QMETHOD_HOUR['3'], QMETHOD_HOUR['2'],
		            QMETHOD_HOUR['1'], QMETHOD_HOUR['0']
		        ]
		    },
		    {
		        name: '所有总数',
		        type: 'line',
		        stack: '总量',
		        data: [
		            MPQ_HOUR['24'], MPQ_HOUR['23'], MPQ_HOUR['22'],
		            MPQ_HOUR['21'], MPQ_HOUR['20'], MPQ_HOUR['19'],
		            MPQ_HOUR['18'], MPQ_HOUR['17'], MPQ_HOUR['16'],
		            MPQ_HOUR['15'], MPQ_HOUR['14'], MPQ_HOUR['13'],
		            MPQ_HOUR['12'], MPQ_HOUR['11'], MPQ_HOUR['10'],
		            MPQ_HOUR['9'], MPQ_HOUR['8'], MPQ_HOUR['7'],
		            MPQ_HOUR['6'], MPQ_HOUR['5'], MPQ_HOUR['4'],
		            MPQ_HOUR['3'], MPQ_HOUR['2'],
		            MPQ_HOUR['1'], MPQ_HOUR['0']
				]
		    }
		]
	};
	return option;
}

function disDayChart(ec) {
	var lineChart = ec.init(document.getElementById('showview'));
	var lineChartOtion = getDayOption();	
	lineChart.setOption(lineChartOtion, true); 
}

/**
 * 获得Line图的选项和数据
 * @returns {___anonymous4970_6233}
 */
function getDayOption() {
	var option = {
		title: {
			text: mapsTitle[shooseTypeid],
			subtext: '',
			x: 'left'
		},
		tooltip: {trigger: 'axis'},
		legend: {data:['PC端', '移动端', '二维码', '所有总数']},
		toolbox: {
			title : {
				text: mapsTitle[shooseTypeid],
			    subtext: '',
			    x: 'left'
			},
			show: true,
			feature: {
				mark: {show: true},
				dataView: {show: true, readOnly: false},
				magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
				restore: {show: true},
				saveAsImage: {show: true}
			}
		},
		calculable: true,
		xAxis: [{
			type: 'category',
			boundaryGap: false,
			axisLabel: {
				interval: 1,  
				rotate: 40
			},
			data: listDayFiled
		}],
		yAxis: [{type : 'value'}],
		series: [
		    {name: 'PC端', type: 'line', data: listDateP},
		    {name: '移动端', type: 'line', data: listDateM},
		    {name: '二维码', type: 'line', data: listDateQ},
		    {name: '所有总数', type: 'line', data: listDateMPQ} 
		]
	};
	return option;
}

function sendAjax() {
	intn(24, "小时");
	//var qrtype=$("input[name=n1]:checked").val();
	var loadLayerindex = layer.load(1);
	$.ajax({
		url: path + '/totalController/searchTotalHour',
		type: "post",
		dataType: "json",
		success: function(data) {
			PMETHOD_HOUR['0'] = 0 ;
			MMETHOD_HOUR ['0'] = 0;
			QMETHOD_HOUR['0'] = 0;
			MPQ_HOUR['0'] = 0;
			for (var i = 0; i < data.length; i ++) {
				if(data[i]['stype'] == "P") {
					PMETHOD_HOUR[data[i]['timer']] = PMETHOD_HOUR[data[i]['timer']] + 1;
				} else if(data[i]['stype'] == "M") {
					MMETHOD_HOUR[data[i]['timer']] = MMETHOD_HOUR[data[i]['timer']] + 1;
				} else if(data[i]['stype'] == "Q") {
					QMETHOD_HOUR[data[i]['timer']] = QMETHOD_HOUR[data[i]['timer']] + 1;
				}
			}
			for (var i = 24; i >= 0; i --) {
				MPQ_HOUR[i + ""] = (QMETHOD_HOUR[i + ""] + MMETHOD_HOUR[i + ""] + PMETHOD_HOUR[i + ""]);
			}
			require(['echarts', 'echarts/chart/line'], displayChart);
			layer.close(loadLayerindex); 
		},
       	error: function() {
			layer.close(loadLayerindex); 
			layer.alert("操作失败"); 
		}
	});	
}

function ajaxHour24(valueFtype) {
	intn(24, "小时");
	var ftype = valueFtype;
	if(ftype == null) {
		ftype = "";
	}
	var pid = $("#searId").val();
	if(pid == null || pid == "") {
		pid = 0;
	}
	var loadLayerindex = layer.load(1);
	$.ajax({
		url: path + '/totalController/searchTotalIndex',
		type: "post",
		dataType: "json",
		data: {
			ftype: ftype,
			pid: pid
		},
		success: function(data) {
			PMETHOD_HOUR['0'] = 0 ;
			MMETHOD_HOUR ['0'] = 0;
			QMETHOD_HOUR['0'] = 0;
			MPQ_HOUR['0'] = 0;
			for (var i = 0; i < data.length; i ++) {
				if (data[i]['stype'] == "P") { 
					PMETHOD_HOUR[data[i]['timer']] = PMETHOD_HOUR[data[i]['timer']] + 1;
				} else if(data[i]['stype'] == "M"){
					MMETHOD_HOUR[data[i]['timer']] = MMETHOD_HOUR[data[i]['timer']] + 1;
				} else  if(data[i]['stype'] == "Q"){ 
					QMETHOD_HOUR[data[i]['timer']] = QMETHOD_HOUR[data[i]['timer']] + 1;
				}
			}
			for (var i = 24; i >= 0; i --) {
				MPQ_HOUR[i + ""] = (QMETHOD_HOUR[i + ""] + MMETHOD_HOUR[i + ""] + PMETHOD_HOUR[i + ""]);
			}
			require(['echarts', 'echarts/chart/line'], displayChart);
			layer.close(loadLayerindex);
		},
		error: function() {
			layer.close(loadLayerindex);
			layer.alert("操作失败"); 
		}
	});	
}

function sendAjaxIndex(valueFtype) {
	if(valueFtype == 1 && shooseTypeid < 2) {
		$("#showId").show();
		$("#searId").val("");
		$("#showDayId").hide();
		$("#dayId").val("");
	}
	if(valueFtype == "" && shooseTypeid < 2) {
		$("#showId").hide();
		$("#searId").val("");
		$("#showDayId").hide();
		$("#dayId").val("");
	}
	if(valueFtype == 0 && shooseTypeid < 2) {
		$("#showId").hide();
		$("#searId").val("");
		$("#showDayId").hide();
		$("#dayId").val("");
	}
	if(valueFtype == 1 && shooseTypeid > 1) {
		$("#showDayId").show();
		$("#searId").val("");
		$("#showId").hide();
		$("#dayId").val("");
	}
	if(valueFtype == "" && shooseTypeid > 1) {
		$("#showDayId").hide();
		$("#searId").val("");
		$("#showId").hide();
		$("#dayId").val("");
	}
	if(valueFtype == 0 && shooseTypeid > 1) {
		$("#showDayId").hide();
		$("#searId").val("");
		$("#showId").hide();
		$("#dayId").val("");
	}
	if(shooseTypeid == 1) {
		ajaxHour24(valueFtype);
	}
	if(shooseTypeid == 2) {
		weekDayAjax(7, "天" );
	}
	if(shooseTypeid == 3) {
		weekDayAjax(15, "天" );
	}
	if(shooseTypeid == 4) {
		weekDayAjax(30, "天" );
	}
}

function chooseType(id) {
	var flag = true; // 是否点击了相同的tab页
	$("#typeid").val(id)
	if(shooseTypeid != id) {
		flag = false;
		$("#ftype").val("");
		$("#showId").hide(); 
		$("#showTime").hide();
		$("#showDayId").hide();
		$("#showDaybutton").hide();
		if(shooseTypeid == 1) {
			$("#searId").val("");
		}
	}
	shooseTypeid = id;
	/* ------ 切换样式 ----------*/
	$("#total" + id).attr("class", "citySel"); 
	for (var i = 1; i < 6; i ++) {
		if(i != id) {
			$("#total" + i).removeAttr("class");
		}
	}
	if(id == 1) {
		if(!flag) {
			$("#showId").hide(); 
			$("#showTime").hide();
		}
		ajaxHour24();
	}
	if(id == 2) {
		 $("#showDaybutton").show();
		 $("#showId").hide(); 
		 $("#showTime").hide();
		 weekDayAjax(7, "天");
	}
	if(id == 3) {
		$("#showDaybutton").show();
		$("#showId").hide(); 
		$("#showTime").hide();
		weekDayAjax(15, "天");
	}
	if(id == 4) {
		$("#showDaybutton").show();
		$("#showId").hide(); 
		$("#showTime").hide();
		weekDayAjax(30, "天");
	}
	
	if(id == 5) {
		//$("#showDayId").show();
		$("#showId").hide(); 
		$("#showTime").show();
	} else {
		//$("#showDayId").hide();
	}
}

//显示 时间段为天数的数据 
function weekDayAjax(day, text) {
	listDateFiled = new Array(); 
	var pid = $("#dayId").val();
	intn(day, text);
	var ftype = $("#ftype").val();
	if(pid == "" || pid == null) {
		pid = 0;
	}	
	var loadLayerindex = layer.load(1);
	$.ajax({
		url: path + '/totalController/searchTotalWeekDay',
		type: "post",
		dataType: "json",
		data: {
			ftype: ftype,
			day: day,
			pid: pid
		},
		error: function() {
			layer.close(loadLayerindex); 
			layer.alert("操作失败"); 
		},
		success: function(data) {
			for (var i = 0; i < data.length; i ++) {
				PMETHOD_HOUR[data[i]['timer']] = PMETHOD_HOUR[data[i]['timer']] + data[i]['pc'] ;
				MMETHOD_HOUR[data[i]['timer']] = MMETHOD_HOUR[data[i]['timer']] + data[i]['mobile'] ;
				QMETHOD_HOUR[data[i]['timer']] = QMETHOD_HOUR[data[i]['timer']] + data[i]['qrcode'] ;
			}
			/*-------------- echats  使用倒序 记录数据 --------------- */
			var index = 0;
			for (var i = day - 1; i >= 0; i --) {
				listDateM[index] = MMETHOD_HOUR[(i + 1) + ""] ;
				listDateP[index] = PMETHOD_HOUR[(i + 1) + ""]  ;
				listDateQ[index] = QMETHOD_HOUR[(i + 1) + ""] ;
				listDateMPQ[index] = MMETHOD_HOUR[(i + 1) + ""] + PMETHOD_HOUR[(i + 1) + ""] + QMETHOD_HOUR[(i + 1) + ""];
				index = index + 1;
			}
			for (var i = 0; i < day; i++) {
				var dat= new Date();
				var dateDay = addDate(dat, day - i);
				listDayFiled[i] = dateDay;
			}
			// 作为入口
			require(['echarts', 'echarts/chart/line'], disDayChart);
			layer.close(loadLayerindex); 
		}
	});	
}

//显示 自定义时间查询的数据
function totlaDayAjax(day, text, startDate, endDate) {
	listDateFiled = new Array(); 
	var pid = $("#dayId").val();
	intn(day, text);
	var ftype = $("#ftype").val();
	if(pid == "" || pid == null) {
		pid = 0;
	}
	var loadLayerindex = layer.load(1);
	$.ajax({
		url: path + '/totalController/searchTotalDay',
		type: "post",
		dataType: "json",
		data: {
			ftype: ftype,
			day: day,
			pid: pid,
			startDate: startDate,
			endDate: endDate
		},
		error: function() {
			layer.close(loadLayerindex); 
			layer.alert("操作失败"); 
		},
		success: function(data) {
			for (var i = 0; i < data.length; i ++) {
				PMETHOD_HOUR[data[i]['timer']] = PMETHOD_HOUR[data[i]['timer']] + data[i]['pc'] ;
				MMETHOD_HOUR[data[i]['timer']] = MMETHOD_HOUR[data[i]['timer']] + data[i]['mobile'] ;
				QMETHOD_HOUR[data[i]['timer']] = QMETHOD_HOUR[data[i]['timer']] + data[i]['qrcode'] ;
			}
			/*-------------- echats  使用倒序 记录数据 --------------- */
			var index=0;
			for (var i = day - 1; i >= 0; i --) {
				listDateM[index] = MMETHOD_HOUR[(i + 1) + ""] ;
				listDateP[index] = PMETHOD_HOUR[(i + 1) + ""]  ;
				listDateQ[index] = QMETHOD_HOUR[(i + 1) + ""] ;
				listDateMPQ[index] = MMETHOD_HOUR[(i + 1) + ""] + PMETHOD_HOUR[(i + 1) + ""] + QMETHOD_HOUR[(i + 1) + ""];
				index = index + 1;
			}
			for (var i = 0; i < day; i ++) {
				var dat = new Date(startDate)
				var dateDay = aDate(dat, i);
				listDayFiled[i] = dateDay;
			}
			// 作为入口
			require(['echarts', 'echarts/chart/line'], disDayChart);
			layer.close(loadLayerindex); 
		}
	});	
}

function sumDay() {
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	var dat1 = new Date(startDate);
	var dat2 = new Date(endDate);
	if(startDate == "") {
		layer.alert("请选择开始时间");
		return ;
	}
	if(endDate == "") {
		layer.alert("请选择结束时间");
		return ;
	}
	if(dat2 - dat1 < 0) {
		layer.alert('请选择正确的时间段');
		return ;
	}
	var dat3 = dat2 - dat1;
	var day = dat3 / (24 * 3600 * 1000);
	totlaDayAjax(day, "天", startDate, endDate);
} 

function submitePid() {
	ajaxHour24(1);
}

function submiteDayPid() {
	if(shooseTypeid == 5) {
		sumDay();
	} 
	if(shooseTypeid == 2) {
		weekDayAjax(7,"天" );
	}
	if(shooseTypeid == 3) {
		weekDayAjax(15,"天" );
	}
	if(shooseTypeid == 4) {
		weekDayAjax(30,"天" );
	}
}

function exploerData() {
	$("#dataForm").submit();
}

function addDate(date, days) { 
    var d = new Date(date); 
    d.setDate(d.getDate() - days); 
    var m = d.getMonth() + 1; 
    return d.getFullYear() + '-' + m + '-' + d.getDate(); 
}

function aDate(date, days) { 
    var d = new Date(date); 
    d.setDate(d.getDate() + days); 
    var m = d.getMonth() + 1; 
    return d.getFullYear() + '-' + m + '-' + d.getDate(); 
}

function getThisTime(date, time) { 
    var d = new Date(date); 
    d.setHours(d.getHours() + time, 00, 00, 00);
    var m = d.getMonth() + 1; 
    return d.getHours() + ':00:00'; 
}

// 对Date的扩展，将 Date 转化为指定格式的String   
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，   
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)   
// 例子：   
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423   
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18   
Date.prototype.Format = function(fmt){
  var o = {
    "M+" : this.getMonth()+1,                 //月份   
    "d+" : this.getDate(),                    //日   
    "h+" : this.getHours(),                   //小时   
    "m+" : this.getMinutes(),                 //分   
    "s+" : this.getSeconds(),                 //秒   
    "q+" : Math.floor((this.getMonth()+3)/3), //季度   
    "S"  : this.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}
